package h0903;

import java.util.HashSet;
import java.util.Set;

/**
  * @description 是否是快乐数
  * @author 不知名帅哥
  * @date 2024/9/3 17:00
  * @version 1.0
*/
public class HappyNum {
    public static void main(String[] args) {
        System.out.println(isHappy(91));
    }
    public static boolean isHappy(int n) {
        Set<Integer> seen=new HashSet<>();
        while (n!=1&&!seen.contains(n)){
            seen.add(n);
            n=getNext(n);
        }
        return n==1;
    }

    private static int getNext(int n) {
        int remain=0;
        int sum=0;
        while (n!=0){
            remain=n%10;
            sum+=remain*remain;
            n/=10;
        }
        return sum;
    }
}
